Django 本身就有蠻多好用的指令,像是
- check
 
- compilemessages
 
- createcachetable
 
- dbshell
 
- diffsettings
 
- dumpdata
 
- flush
 
- inspectdb
 
- loaddata
 
- makemessages
 
- makemigrations
 
- migrate
 
- sendtestemail
 
- shell
 
- showmigrations
 
- sqlflush
 
- sqlmigrate
 
- sqlsequencereset
 
- squashmigrations
 
- startapp
 
- startproject
 
- test
 
- testserver
 
但真正到用的時候,總是會覺得少了點什麼,礙手礙腳的,例如:看有 expose 哪些 url 出來、建立 template tags/command/job、清除 .pyc、依據 model 產生關聯圖...等等的,而這些,django-extensions 都有提供。
安裝
poetry add django-extensions
設定
# settings
INSTALLED_APPS = [
  # ...
  'django_extensions',
]
使用
執行 poetry run python [manage.py](http://manage.py) --help 就可以看到能用的指令了。
django-extensions 提供以下指令:
- admin_generator:為 app 產生 admin.py
 
- clean_pyc / clear_cache:清除 .pyc 跟清除 cache ,清除 cache 是依據 settings 裡的設定。
 
- compile_pyc:預先將 .py 編譯為 .pyc
 
- create_command / create_jobs / create_template_tags:產生指令、工作跟 template tags;指令是指輸入 
python [manage.py](http://manage.py) --help 時會出現的指令。 
- delete_squashed_migrations:移除已經合併的 migration
 
- describe_form:指定 model 以後,幫你產生 form 
poetry run python [manage.py](http://manage.py) describe_form news.Article
 
- drop_test_database:移除測試資料庫。
 
- dumpscript:產生匯入資料用的腳本,假設是把腳本輸出到 scripts/xxx.script 的話,那就可以用 
poetry run python [manage.py](http://manage.py) runscript scripts/xxx.script 來執行。 
- export_emails:匯出使用者的電子郵件信箱
 
- find_template:尋找 template 檔案,你知道的,有時候就是會忘記放在哪...
 
- generate_password:產生一組密碼
 
- generate_secret_key:產生 secret key,這個 secret key 就可以填到 settings 裡
 
- graph_models:依據 model 產出 model 的關聯圖。
 
- list_model_info:印出 model 的欄位以及方法。
 
- list_signals:印出自訂的 signal (觸發程序)
 
- mail_debug:啟動測試用的郵件伺服器
 
- merge_model_instances:找出重複的個體 (如果你對資料表格比較熟悉的話,就是重複的記錄),並加以合併
 
- notes:找出程式碼裡所有的 TODO, FIXME, BUG, HACK, NOTE, WARNING...等等的註解位置
 
- pipchecker:依據 requirements.txt 去檢查套件是否過期,並標示出來
 
- print_settings:印出 settings
 
- print_user_for_session:從 session id 找到 session 擁有者是誰。
 
- reset_db:重置資料庫
 
- reset_schema:重置資料庫綱要
 
- runjob:執行工作 (job)
 
- runjobs:執行多個工作
 
- runprofileserver:啟動 profiling 用的版本,profiling 是用來評估效能用的,為了安插紀錄點,通常 profiling 用的版本會比較慢,但也能因此得知哪些程序所耗用的時間最多。
 
- runscript:執行腳本
 
- runserver_plus:把這個想成進階版的 runserver ,提供更多的除錯功能。
 
- set_default_site:設定預設站台 (搭配 django site framework)
 
- set_fake_emails:設定假造的電子郵件信箱
 
- set_fake_passwords:設定假造的密碼
 
- shell_plus:進階版的 shell,這主要是使用 ipython,所以有自動完成等的功能。
 
- show_template_tags:顯示 template tags
 
- show_urls:顯示有哪些 url
 
- sqlcreate:產生建立資料庫的 SQL 。
 
- sqldiff:產生資料庫差異的 SQL。
 
- sqldsn:依據指定的資料庫印出 DSN (Database Source Name),不指定資料庫的話,預設的資料庫是 settings 裡的 default
 
- sync_s3:將 MEDIA_ROOT 所指定的資料夾同步到 S3 上去,要使用這指令得再安裝 boto 以及確定 AWS S3 bucket 跟 IAM 權限。
 
- syncdata:讓資料庫裡的資料跟 fixture 一致,fixture 是初始資料。
 
- unreferenced_files:找出沒有被資料庫引用的檔案。一般來說,上傳圖片或檔案,真正的檔案會放到 MEDIA_ROOT 去,資料庫裡只記載相對於 MEDIA_ROOT 的位置,在資料庫裡的記錄刪除以後,檔案不一定會被刪除掉,這時候就需要這個指令來輔助。
 
- update_permissions:更新權限,權限的部份可以參考 MDN Django tutorial part 8
 
- validate_templates:檢查 template 有沒有語法錯誤
 
結語
我自己只用過 graph_models、show_urls、shell_plus、runscript、runserver_plus、runprofileserver 這幾個,剛好趁著這次介紹套件,又多認識了 django-extensions 一點。